package com.store.practice.controller;

import com.store.practice.common.api.CommonPage;
import com.store.practice.common.api.CommonResult;
import com.store.practice.dto.BgDptInsertParam;
import com.store.practice.model.BgDepartment;
import com.store.practice.model.BgPosition;
import com.store.practice.service.BgDepartmentService;
import com.store.practice.service.BgPositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@Api(tags = "BgDepartmentController",description = "部门管理")
@RequestMapping("/bgDpt")
public class BgDepartmentController {
    @Autowired
    private BgDepartmentService departmentService;
    @Autowired
    private BgPositionService positionService;

    @ApiOperation("新增部门")
    @PostMapping("/insert")
    public CommonResult insert(@RequestBody BgDptInsertParam param){
        int i = departmentService.insert(param);
        if(i>0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改部门信息")
    @PostMapping("/update")
    public CommonResult update(Integer id,@RequestBody BgDptInsertParam param){
        int i = departmentService.update(id,param);
        if (i > 0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation("展示部门列表信息")
    @GetMapping("/list")
    public CommonResult<CommonPage<BgDepartment>> list(){
        List<BgDepartment> list = departmentService.list();
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("新增部门职位")
    @PostMapping("/insertPosition")
    public CommonResult insertPosition(Integer dptId,@RequestBody BgPosition position){
        int i = positionService.insertPosition(dptId, position);
        if (i>0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }
    @ApiOperation("编辑部门职位信息")
    @PostMapping("/updatePosition")
    public CommonResult updatePosition(@RequestBody BgPosition position){
        int i = positionService.updatePosition(position);
        if (i>0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation("根据部门id获取职位名称列表")
    @GetMapping("/listPosition")
    public CommonResult getByDptId(Integer dptId){
        List<BgPosition> list = positionService.getByDptId(dptId);
        if (list!=null){
            return CommonResult.success(CommonPage.restPage(list));
        }
        return CommonResult.failed();
    }
}
